package com.example.sls.dao.social;

import com.example.sls.pojo.social.FriendList;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 好友列表(FriendList)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:54:57
 */
public interface FriendListDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from friend_list where id=#{id}")
  FriendList queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param friendList 查询条件
   * @param pageSize   分页对象
   * @param offset     偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from friend_list where 1=1" +
          " and (id=#{friendList.id} or (isnull(#{friendList.id}) or #{friendList.id}=0))" +
          " and (user_id=#{friendList.userId} or (isnull(#{friendList.userId}) or #{friendList.userId}=0))" +
          " and (friend_id=#{friendList.friendId} or (isnull(#{friendList.friendId}) or #{friendList.friendId}=0))" +
          " and (friend_name=#{friendList.friendName} or (isnull(#{friendList.friendName}) or #{friendList.friendName}=''))" +
          " and (friend_avatar=#{friendList.friendAvatar} or (isnull(#{friendList.friendAvatar}) or #{friendList.friendAvatar}=''))" +
          " and (friend_phone=#{friendList.friendPhone} or (isnull(#{friendList.friendPhone}) or #{friendList.friendPhone}=''))" +
          " and (friend_major=#{friendList.friendMajor} or (isnull(#{friendList.friendMajor}) or #{friendList.friendMajor}=''))" +
          " and (friend_gender=#{friendList.friendGender} or (isnull(#{friendList.friendGender}) or #{friendList.friendGender}=''))" +
          " and (created_at=#{friendList.createdAt} or (isnull(#{friendList.createdAt}) or #{friendList.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<FriendList> queryAllByLimit(FriendList friendList, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from friend_list")
  long count();

  /**
   * 新增数据
   *
   * @param friendList 实例对象
   * @return 影响行数
   */
  @Insert("insert into friend_list (id,user_id,friend_id,friend_name,friend_avatar,friend_phone,friend_major,friend_gender,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{userId}!=0 and !isnull(#{userId}),#{userId},null)," +
          "if(#{friendId}!=0 and !isnull(#{friendId}),#{friendId},null)," +
          "if(#{friendName}!='' and !isnull(#{friendName}),#{friendName},null)," +
          "if(#{friendAvatar}!='' and !isnull(#{friendAvatar}),#{friendAvatar},null)," +
          "if(#{friendPhone}!='' and !isnull(#{friendPhone}),#{friendPhone},null)," +
          "if(#{friendMajor}!='' and !isnull(#{friendMajor}),#{friendMajor},null)," +
          "if(#{friendGender}!='' and !isnull(#{friendGender}),#{friendGender},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(FriendList friendList);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param friendList 实例对象
   * @return 影响行数
   */
  @Update("update friend_list set " +
          "user_id=if(isnull(#{userId}) or #{userId}=0,user_id,#{userId})," +
          "friend_id=if(isnull(#{friendId}) or #{friendId}=0,friend_id,#{friendId})," +
          "friend_name=if(isnull(#{friendName}) or #{friendName}='',friend_name,#{friendName})," +
          "friend_avatar=if(isnull(#{friendAvatar}) or #{friendAvatar}='',friend_avatar,#{friendAvatar})," +
          "friend_phone=if(isnull(#{friendPhone}) or #{friendPhone}='',friend_phone,#{friendPhone})," +
          "friend_major=if(isnull(#{friendMajor}) or #{friendMajor}='',friend_major,#{friendMajor})," +
          "friend_gender=if(isnull(#{friendGender}) or #{friendGender}='',friend_gender,#{friendGender})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(FriendList friendList);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from friend_list where id=#{id}")
  int deleteById(Integer id);
}

